package com.datamation.swadeshi.control.data;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.datamation.swadeshi.model.Discslab;

public class DiscslabDS {
	
	private SQLiteDatabase dB;
	private DatabaseHelper dbHelper;
	Context context;
	private String TAG="swadeshi";
	
	
	public DiscslabDS (Context context){
		
		this.context = context;
		dbHelper = new DatabaseHelper(context);
		
	}
	
	public void open() throws SQLException {
		dB = dbHelper.getWritableDatabase();
	}
	
	@SuppressWarnings("static-access")
	public int createOrUpdateDiscslab(ArrayList<Discslab>  list) {
		int count =0;		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		
	try{
		for (Discslab discslab   : list) {
			
		ContentValues values = new ContentValues();
	
		values.put(dbHelper.FDISCSLAB_REF_NO, discslab.getFDISCSLAB_REF_NO());
		values.put(dbHelper.FDISCSLAB_SEQ_NO, discslab.getFDISCSLAB_SEQ_NO());
		values.put(dbHelper.FDISCSLAB_QTY_F, discslab.getFDISCSLAB_QTY_F());
		values.put(dbHelper.FDISCSLAB_QTY_T, discslab.getFDISCSLAB_QTY_T());
		values.put(dbHelper.FDISCSLAB_DIS_PER, discslab.getFDISCSLAB_DIS_PER());
		values.put(dbHelper.FDISCSLAB_DIS_AMUT, discslab.getFDISCSLAB_DIS_AMUT());
		values.put(dbHelper.FDISCSLAB_RECORD_ID, discslab.getFDISCSLAB_RECORD_ID());
		values.put(dbHelper.FDISCSLAB_TIMESTAMP_COLUMN, discslab.getFDISCSLAB_TIMESTAMP_COLUMN());
		
		count = (int) dB.insert(dbHelper.TABLE_FDISCSLAB, null, values);
	
		}
		}finally {  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		return count;
	}
	
	@SuppressWarnings("static-access")
	public int deleteAll(){
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		try{
			
			cursor = dB.rawQuery("SELECT * FROM " + dbHelper.TABLE_FDISCSLAB, null);
			count =cursor.getCount();
			if(count>0){
				int success = dB.delete(dbHelper.TABLE_FDISCSLAB, null, null);
				Log.v("Success", success+"");
			}
		}catch (Exception e){

			Log.v(TAG+" Exception", e.toString());
			
		}finally{  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		
		return count;
		
	}
}
